package com.guguskins.back.utils.steam;

import com.alibaba.fastjson.JSONObject;
import com.guguskins.back.utils.steam.Bot.SteamBotResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * @date 2021/7/26 15:31
 */
@Component
@Slf4j
public class SteamBotUtil {

    private static RestTemplate restTemplate;

    //    private static final String URI = "URI";
    private static final String URI = "https://guguskin.steamrobot.me/api/steambot/";

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        SteamBotUtil.restTemplate = restTemplate;
    }

    /**
     * 机器人状态
     */
    public static SteamBotResponse list() {
        String url = URI + "list/";
        //发起一个GetT请求
        ResponseEntity<SteamBotResponse> responseEntity = restTemplate.getForEntity(url, SteamBotResponse.class);
        return responseEntity.getBody();
    }

    /**
     * 发起交易
     *
     * @param ornaments      要交易的我方饰品列表 key是my_iteams
     * @param transactionBot 发起交易的机器人steamid
     * @param tradeurl       对方的steam报价链接可以为null
     * @param cancelTime     超时时间
     * @param message        steam报价单附带的报价描述信息,
     * @param uid            订单唯一Id
     */
    public static SteamBotResponse trade(JSONObject jsonObject) {
        String url = URI + "trade/";
        //设置Http的Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(jsonObject, headers);

        ResponseEntity<SteamBotResponse> result = restTemplate.exchange(url, HttpMethod.POST, httpEntity, SteamBotResponse.class);
        return result.getBody();
    }

    /**
     * 查询订单
     *
     * @param uid 订单id
     */
    public static JSONObject trade(String uid) {
        String url = URI + "trade/";
        ResponseEntity<String> result = null;
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(uid);
        try {
            //发起一个GetT请求
            result = restTemplate.exchange(url, HttpMethod.GET, stringHttpEntity, String.class);
            JSONObject data = JSONObject.parseObject(result.getBody()).getJSONObject("data");
            return data;
        } catch (Exception e) {
            log.error("查询订单状态失败", e);
        }
        return null;
    }

    /**
     * 取消订单
     *
     * @param uid 订单id
     * @return
     */

    public static JSONObject canceltrade(String uid) {
        String url = URI + "canceltrade/";
        //设置Http的Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        ResponseEntity<String> result = null;
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(uid);
        //发起一个Post请求
        result = restTemplate.exchange(url, HttpMethod.POST, stringHttpEntity, String.class);
        JSONObject data = JSONObject.parseObject(result.getBody()).getJSONObject("data");
//        log.log(Level.valueOf("取消订单失败"), e.getMessage());
        return data;
    }

    /**
     * 获取账号登陆手机令牌
     *
     * @param account steam账号
     */
    public static JSONObject authcode(String account) {
        String url = URI + "canceltrade/";
        //设置Http的Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        ResponseEntity<String> result = null;
        HttpEntity<String> stringHttpEntity = new HttpEntity<>(account);
        try {
            //发起一个Post请求
            result = restTemplate.exchange(url, HttpMethod.POST, stringHttpEntity, String.class);
            JSONObject data = JSONObject.parseObject(result.getBody()).getJSONObject("data");
            return data;
        } catch (Exception e) {
            log.error("获取令牌失败", e);
        }
        return null;
    }

    /**
     * 添加账号
     *
     * @param account        steam账号
     * @param password       密码
     * @param sharedSecret   帐号SDA令牌maFile文件中的移动令牌秘钥
     * @param identitySecret 帐号SDA令牌maFile文件中的报价确认密钥
     */
    public static SteamBotResponse accountAdd(String account, String password, String sharedSecret, String identitySecret) {

        String url = URI + "account/add/";
        //设置Http的Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //发起一个Post请求
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("account", account);
        requestEntity.add("password", password);
        requestEntity.add("shared_secret", sharedSecret);
        requestEntity.add("identity_secret", identitySecret);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(requestEntity, headers);

        ResponseEntity<SteamBotResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, SteamBotResponse.class);
        return responseEntity.getBody();
    }

    /**
     * 移除steam账号要一个steam账号
     *
     * @param account 账号
     */
    public static SteamBotResponse accountRemove(String account) {
        String url = URI + "account/remove/";
        //设置Http的Header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("account", account);

        HttpEntity<MultiValueMap<String, Object>> stringHttpEntity = new HttpEntity<>(requestEntity, headers);
        //发起一个Post请求
        ResponseEntity<SteamBotResponse> responseEntity = restTemplate.postForEntity(url, stringHttpEntity, SteamBotResponse.class);
        return responseEntity.getBody();
    }
}
